package com.jservice.api;

import com.alibaba.fastjson.JSON;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;
import com.jfinal.weixin.sdk.kit.IpKit;
import com.jservice.api.dto.PhoneInfo;
import com.jservice.api.dto.Request;
import com.jservice.api.dto.Response;
import com.jservice.api.dto.ResponseHeader;
import com.jservice.api.service.IService;
import com.jservice.framework.annotation.RouteBind;
import com.jservice.framework.spring.SpringContextUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.log4j.Logger;

import java.util.Date;

/**
 * 通过spring反射调用服务
 */
@RouteBind(path = "/smk_app")
public class ApiController extends Controller{

	public static final Logger logger = Logger.getLogger(ApiController.class);


	public void api() {
		Date date = new Date();
		String content = getPara("content");
		String method = getRequest().getMethod().toUpperCase();
		// 解密报文
		Response response = new Response();
		ResponseHeader header = new ResponseHeader();

		Request r = new Request();
		header.setTrdate(DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));

		// 请求为空
		if (StringUtils.isBlank(content)) {
			header.setErrorcode(MsgCode.Code_1001);
			header.setSuccflag(Response.FAIL);
			response.setHeader(header);
			renderJson(response);
			return;
		}

		// 检查结果
		try {
			r = JSON.parseObject(content, Request.class);
		} catch (Exception e) {
			// 请求格式错误
			header.setTrcode("CODE1002");
			header.setErrormsg("请求格式错误");
			header.setErrorcode(MsgCode.Code_1002);
			header.setSuccflag(Response.FAIL);
			response.setHeader(header);
			renderJson(response);
			return;
		}

		PhoneInfo phoneInfo = r.getHeader().getPhone_info();

		if(phoneInfo == null){
			// 设备信息为空
			header.setTrcode("CODE1004");
			header.setErrorcode(MsgCode.Code_1004);
			header.setErrormsg("设备信息为空");
			header.setSuccflag(Response.FAIL);
			response.setHeader(header);
			renderJson(response);
			return;
		}

		header.setTrcode(r.getHeader().getTrcode());
		header.setAppseq(r.getHeader().getAppseq());

		String ip = IpKit.getRealIp(getRequest());
		if (StrKit.isBlank(ip)) {
			ip = "127.0.0.1";
		}

		r.getHeader().getPhone_info().setIp(ip);

		IService service = null;
		try {
			service = SpringContextUtil.getBean(r.getHeader().getTrcode());
		} catch (Exception e) {
			header.setErrorcode(MsgCode.Code_1003);// 未找到服务号
			header.setErrormsg("未找到服务号,请检查trcode");
			header.setSuccflag(Response.FAIL);
			response.setHeader(header);
			renderJson(response);
			return;
		}
		try {
			response = service.excute(r, response, header, method);
		} catch (Exception e) {
			e.printStackTrace();
			header.setErrorcode(MsgCode.Code_1005);// 系统异常
			header.setSuccflag(Response.FAIL);
			header.setErrormsg(e.getMessage());
			response.setHeader(header);
			renderJson(response);
			return;
		}
		renderJson(response);
		Date over = new Date();
		long execute_time = over.getTime() - date.getTime();
		logger.debug(r.getHeader().getTrcode() + " execute time is :" + execute_time + " ms");
		return;
	}

}
